<script setup lang="ts">
import { computed, defineEmits, defineProps, ref, toRefs } from 'vue'
import { useI18n } from 'vue-i18n'
import BaseImage from '~/components/BaseImage.vue'
import arrowDown from '/images/webp/affiliate-program/arrow-down.webp'
import iconSearch from '/images/webp/notification/icon_search.webp'

// 使用国际化函数
const { t } = useI18n()

interface OptionItem {
  label: string
  value: string
  cur?: string
}

const props = defineProps({
  modelValue: {
    type: String,
    required: true
  },

  options: {
    type: Array as () => OptionItem[],
    required: true
  },

  // 是否显示触发按钮
  showTrigger: {
    type: Boolean,
    default: true
  },

  // 是否支持搜索
  searchable: {
    type: Boolean,
    default: false
  }
})

const { modelValue, options, showTrigger, searchable } = toRefs(props)

const emit = defineEmits(['update:modelValue'])

// 控制显示选择器
const showSelect = ref(false)

// 搜索文本
const searchText = ref('')

// 选择选项处理函数
const selectOption = (option: OptionItem) => {
  emit('update:modelValue', option.value)
  showSelect.value = false
  searchText.value = ''
}

// 打开选择器时清空搜索
const openSelect = () => {
  showSelect.value = true
  searchText.value = ''
}

// 过滤选项
const filteredOptions = computed(() => {
  if (!searchText.value) return options.value
  return options.value.filter(option =>
    option.label.toLowerCase().includes(searchText.value.toLowerCase())
  )
})

// 获取当前选择项的标签文本
const selectedLabel = computed(() => {
  const option = options.value.find(option => option.value === modelValue.value)
  return option ? option.label : modelValue.value
})

// 默认导出组件
defineExpose({
  showSelect,
  selectOption
})
</script>

<template>
  <div class="app-select">
    <!-- 选择器触发按钮 -->
    <div v-if="showTrigger" class="select-trigger" @click="openSelect">
      <span>{{ selectedLabel }}</span>
      <div class="arrow-icon">
        <BaseImage width="12px" :url="arrowDown" />
      </div>
    </div>

    <!-- 选择器弹出层 -->
    <div v-if="showSelect" class="select-popup-container">
      <div class="popup-mask" @click.stop="showSelect = false" />
      <div class="select-popup animated">
        <div class="popup-content">
          <div class="popup-header">
            <div class="popup-title">选择币种</div>
            <div class="close-btn" @click="showSelect = false">
              <span class="close-icon">×</span>
            </div>
          </div>

          <!-- 搜索框 -->
          <div v-if="searchable" class="search-container">
            <BaseImage width="24px" :url="iconSearch" />
            <input v-model="searchText" type="text" placeholder="搜索" class="search-input" />
          </div>

          <div class="popup-inner">
            <div v-for="option in filteredOptions" :key="option.value" class="select-option"
              :class="{ active: option.value === modelValue }" @click="selectOption(option)">
              <div class="option-content">
                <BaseCurrencyIcon v-if="option.cur" style="width: 14px; height: 14px; margin-right: 8px;"
                  :cur="option.cur" />
                {{ option.label }}
              </div>
              <div class="circle-container">
                <div v-if="option.value === modelValue" class="select-indicator" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.app-select {
  position: relative;
}

.select-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #292D2E;
  border: 1px solid var(---border-black-33B4041, #3B4041);
  border-radius: 8px;
  padding: 9px 15px;
  font-size: 14px;
  cursor: pointer;
  width: 100%;
  min-width: 100px;
  flex: 0 0 auto;
}

// 弹出动画
.popup-enter-active,
.popup-leave-active {
  transition: all 0.3s ease;
}

.popup-enter-from,
.popup-leave-to {
  transform: translateY(100%);
  opacity: 0;
}

// 弹出选择器容器
.select-popup-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 100;

  .popup-mask {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
  }
}

// 弹出选择器样式
.select-popup {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  z-index: 101;

  &.animated {
    animation: slideUp 0.3s ease-out forwards;
  }

  @keyframes slideUp {
    from {
      transform: translateY(100%);
    }

    to {
      transform: translateY(0);
    }
  }

  .popup-content {
    background-color: #232626;
    border-radius: 12px 12px 0 0;
    position: relative;
  }

  .popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    position: relative;

    .popup-title {
      font-size: 16px;
      font-weight: bold;
      color: #fff;
    }

    .close-btn {
      width: 28px;
      height: 28px;
      background: #4a5354;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 6px;

      .close-icon {
        font-size: 14px;
        font-weight: 700;
        color: #fff;
      }
    }
  }

  // 搜索框样式
  .search-container {
    display: flex;
    align-items: center;
    gap: 4px;
    background-color: #323738;
    border-radius: 6px;
    margin: 0 15px 20px;
    padding-left: 15px;
    border: 2px solid var(---border-black-33B4041, #3B4041);
  }

  .search-input {
    width: 100%;
    height: 40px;
    border: none;
    padding-right: 12px;
    color: #B3BEC1;
    font-size: 14px;

    &::placeholder {
      color: #7A8283;
    }
  }

  .popup-inner {
    max-height: 40vh;
    overflow-y: scroll;
    padding: 0 0 20px;
    margin: 0 15px;
  }

  .select-option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 20px;
    font-size: 16px;
    position: relative;
    border-radius: 4px;

    &.active {
      color: white;
      font-weight: 500;
      background-color: #323738;
    }

    .option-content {
      display: flex;
      align-items: center;
    }

    .circle-container {
      width: 24px;
      height: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;

      &::before {
        content: '';
        position: absolute;
        width: 20px;
        height: 20px;
        border: 1px solid #e4eaf030;
        border-radius: 50%;
      }
    }

    .select-indicator {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      background-color: #24ee89;
      position: absolute;

      &::before {
        content: '';
        position: absolute;
        width: 10px;
        height: 10px;
        background: #323738;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        border-radius: 50%;
      }
    }
  }
}

.arrow-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #3a4142;
  border-radius: 4px;
}
</style>
